package com.demo.daoImpl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.demo.dao.UserDao;
import com.demo.model.User;
import com.demo.model.Dog;
import com.demo.util.ConnectionUtil;

public class UserDaoImpl extends ConnectionUtil implements UserDao{

	
	//用户登录
	@Override
	public User login(User user) {
		User newUser = null;
		// 连接数据库
		try {
			openConnection();
			ps = con.prepareStatement("select * from user where user_name=? and user_pwd=?");
			ps.setString(1, user.getUser_name());
			ps.setString(2, user.getUser_pwd());
			
			rs = ps.executeQuery();
			while(rs.next()){
				int uid = rs.getInt("user_id");
				String uname = rs.getString("user_name");
				String upwd = rs.getString("user_pwd");
				newUser = new User(uname, upwd, uid);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return newUser;
		
	}
	
	//管理员登录
	@Override
	public User login1(User user) {
		User newUser = null;
		openConnection();
		try {
			ps = con.prepareStatement("select * from manager where user_name=? and user_pwd=?");
			ps.setString(1, user.getUser_name());
			ps.setString(2, user.getUser_pwd());
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				int uid = rs.getInt("user_id");
				String uname = rs.getString("user_name");
				String upwd = rs.getString("user_pwd");
				
				newUser = new User(uname, upwd, uid);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newUser;
	}

	//用户注册
	@Override
	public int regist(User user) {
		int i = 0;
		openConnection();
		try {
			ps = con.prepareStatement("insert into user (user_name,user_pwd,sex,email,address,mobilephone) values(?,?,?,?,?,?)");
			ps.setString(1, user.getUser_name());
			ps.setString(2, user.getUser_pwd());
			ps.setString(3, user.getSex());
			ps.setString(4, user.getEmail()); 
			ps.setString(5, user.getAddress());
			ps.setString(6, user.getMobilephone());
			
			i = ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	//查看所有用户信息
	@Override
	public List<User> getAlluser() {
		List<User> list;
		list = new ArrayList<User>();
		openConnection();
		try {
			ps = con.prepareStatement("select * from user");
			rs = ps.executeQuery();
			//对结果集进行遍历
			while(rs.next()){
				Integer uid = rs.getInt("user_id");
				String uname = rs.getString("user_name");
				String upwd = rs.getString("user_pwd");
				String sex = rs.getString("sex");
				String email = rs.getString("email");
				String address = rs.getString("address");
				String mobilephone = rs.getString("mobilephone");
				
				User user = new User(uname, upwd, uid, sex, email, address, mobilephone);
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	//根据id值删除用户信息
	@Override
	public int delUser(String userId) {
          openConnection();
          int i = 0;
          try {
			ps = con.prepareStatement("delete from user where user_id=?");
		    ps.setInt(1, Integer.parseInt(userId));
		    
		    i = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return i;  
	}

	//根据id值查询单个用户信息
	@Override
	public User selOne(String userId) {
		User user = null;
		openConnection();
	    try {
			ps = con.prepareStatement("select * from user where user_id=?");
			ps.setInt(1, Integer.parseInt(userId));
			//执行查询操作
			rs = ps.executeQuery();
			//
			while(rs.next()){
				Integer uid = rs.getInt("user_id");
				String uname = rs.getString("user_name");
				String upwd = rs.getString("user_pwd");
				String sex = rs.getString("sex");
				String email = rs.getString("email");
				String address = rs.getString("address");
				String mobilephone = rs.getString("mobilephone");
				
				user = new User(uname, upwd, uid, sex, email, address, mobilephone);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    return user;
		
	}

	//根据id值修改用户信息
	@Override
	public int updateUser(User user) {
		int i = 0;
		openConnection();
		try {
			ps = con.prepareStatement("update user set user_name=?,user_pwd=?,sex=?,email=?,address=?,mobilephone=? where user_id=?");
			//赋值
			ps.setString(1, user.getUser_name());
			ps.setString(2, user.getUser_pwd());
			ps.setString(3, user.getSex());
			ps.setString(4, user.getEmail());
			ps.setString(5, user.getAddress());
			ps.setString(6, user.getMobilephone());
			ps.setInt(7, user.getUser_id());
			i = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}

	//查看所有宠物信息
	@Override
	public List<Dog> getAlldog() {
		List<Dog> list;
		list = new ArrayList<Dog>();
		openConnection();
		try {
			ps = con.prepareStatement("select * from dog");
			rs = ps.executeQuery();
			//对结果集进行遍历
			while(rs.next()){
				Integer uid = rs.getInt("user_id");
				String uname = rs.getString("user_name");
				String age= rs.getString("age");
				String birthday = rs.getString("birthday");
				String type = rs.getString("type");
				String color = rs.getString("color");
				String pic = rs.getString("pic");
				String body_length = rs.getString("body_length");
				String body_weight = rs.getString("body_weight");
				
				Dog dog = new Dog(uid, uname, age, birthday, type, color, pic, body_length, body_weight);
				list.add(dog);
						
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int delDog(String userId) {
		int i = 0;
		openConnection();
		try {
			ps = con.prepareStatement("delete from dog where user_id=?");
			ps.setInt(1, Integer.parseInt(userId));
			i = ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i ;
	}

	@Override
	public Dog selOnedog(String userId) {
		Dog dog = null;
		openConnection();
		try {
			ps = con.prepareStatement("select * from dog where user_id=?");
			ps.setInt(1, Integer.parseInt(userId));
			rs = ps.executeQuery();
			
			while(rs.next()){
				Integer uid = rs.getInt("user_id");
				String uname = rs.getString("user_name");
				String age = rs.getString("age");
				String birthday = rs.getString("birthday");
				String type = rs.getString("type");
				String color = rs.getString("color");
				String pic = rs.getString("pic");
				String body_length = rs.getString("body_length");
				String body_weight = rs.getString("body_weight");
				
				dog = new Dog(uid, uname, age, birthday, type, color, pic, body_length, body_weight);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dog;
	}

	@Override
	public int updateDog(Dog dog) {
		int i = 0;
		openConnection();
		try {
			ps = con.prepareStatement("update dog set user_name=?,age=?,birthday=?,type=?,color=?,pic=?,body_length=?,body_weight=? where user_id=?");
			ps.setString(1, dog.getUser_name());
			ps.setString(2, dog.getAge());
			ps.setString(3, dog.getBirthday());
			ps.setString(4, dog.getType());
			ps.setString(5, dog.getColor());
			ps.setString(6, dog.getPic());
			ps.setString(7, dog.getBody_length());
			ps.setString(8, dog.getBody_weight());
			ps.setInt(9, dog.getUser_id());
			i = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i ;
	}


	@Override
	public int addOnedog(Dog dog) {
		int i = 0;
		openConnection();
		try {
			ps = con.prepareStatement("insert into dog (user_name,age,birthday,type,color,pic,body_length,body_weight) values (?,?,?,?,?,?,?,?)");
			ps.setString(1, dog.getUser_name());
			ps.setString(2, dog.getAge());
			ps.setString(3, dog.getBirthday());
			ps.setString(4, dog.getType());
			ps.setString(5, dog.getColor());
			ps.setString(6, dog.getPic());
			ps.setString(7, dog.getBody_length());
			ps.setString(8, dog.getBody_weight());
			
			i = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i ;
		
	}







}
